var PersonCapacityInfoSearch = function (user) {
  SearchBaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (user) {
      var userType = this.getUserType(user);
      if (userType === 3) {
        return;
      }
      self.userType = userType;

      var deptWork = this.getDeptWork(user);
      if (deptWork === null) {
        return;
      }
      self.deptWork = deptWork;

      var searchType = deptWork.length;
      self.searchType = searchType;
      var searchFields = [{
        valueField: 'processObj.processId',
        searchField: 'processId'
      }, {
        valueField: 'user.userId',
        searchField: 'userId'
      }];
      var propertyList = ['user', 'processObj', 'month', 'type'];
      if (searchType === 0) {
        propertyList.push('dept', 'workshop');
        self.dept = '';
        self.workshop = '';
        searchFields.push({
          valueField: 'dept.deptId',
          searchField: 'deptId'
        }, {
          valueField: 'workshop.workshopId',
          searchField: 'workShopId'
        });
      } else if (searchType === 1) {
        propertyList.push('workshop');
        self.workshop = '';
        searchFields.push({
          valueField: 'workshop.workshopId',
          searchField: 'workShopId'
        });
      }

      self.searchFields = searchFields;
      self.propertyList = propertyList;
      self.user = '';
      self.processObj = '';
      self.month = moment().startOf('month').valueOf();
      self.type = '';
      self.saveOldData();
    },

    getDeptWork: function (user) {
      var userType = self.userType;
      if (userType === 0) {
        return [];
      }

      var deptId = user.deptId;
      if (SysUtil.isEmpty(deptId) || deptId === '0') {
        return null;
      }

      var deptWork = [deptId];
      if (userType === 2) {
        var workShopId = user.workShopId;
        if (!SysUtil.isEmpty(workShopId) && workShopId !== '0') {
          deptWork.push(workShopId);
        }
      }
      return deptWork;
    },

    getUserType: function (user) {
      var permissionList = user.permissionList;
      var isManager = user.isManager;
      if (permissionList.indexOf('/superManager') !== -1) {
        isManager = 1;
      }
      if (isManager === 1) {
        return 0;
      }

      var duty = user.duty;
      if (duty === '1') {
        return 1;
      }
      if (duty === '2') {
        return 2;
      }
      return 3;
    }
  };


  this.getSearchObj = function () {
    var obj = {};
    var fields = this.searchFields;

    for (var i = 0; i < fields.length; i++) {
      var fieldObj = fields[i];
      var value = _.get(this, fieldObj.valueField, '');
      if (SysUtil.isEmpty(value)) {
        continue;
      }
      obj[fieldObj.searchField] = value;
    }

    var type = this.type;
    if (type !== '') {
      obj.type = type;
    }

    if (SysUtil.isEmptyObjct(obj)) {
      return null;
    }
    return obj;
  };

  this.getSearchFields = function () {
    var fields = this.searchFields;
    fields = _.map(fields, 'searchField');
    return fields;
  };

  fn.initData(user);
};

PersonCapacityInfoSearch.typeObj = {
  "1": "人员报工",
  "2": "设备报工"
};

PersonCapacityInfoSearch.getTypeList = function (fields) {
  var typeObj = this.typeObj;
  var typeList = [];
  for (var i = 0; i < fields.length; i++) {
    var field = fields[i];
    if (typeof field !== 'string') {
      field = String(field);
    }
    var value = typeObj[field];
    if (SysUtil.isEmpty(value)) {
      continue;
    }
    typeList.push({
      value: field,
      label: value
    });
  }
  return typeList;
};
